package com.ruoyi.official.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.official.domain.OfficialCategory;
import com.ruoyi.official.domain.OfficialProduct;
import com.ruoyi.official.dto.OfficialProductDTO;
import com.ruoyi.official.dto.SpecificationDTO;
import com.ruoyi.official.mapper.OfficialCategoryMapper;
import com.ruoyi.official.mapper.OfficialProductMapper;
import com.ruoyi.official.service.IOfficialProductService;
import com.ruoyi.official.vo.OfficialProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 产品Service业务层处理
 *
 * @author yuanqiji
 * @date 2025-03-21
 */
@Service
public class OfficialProductServiceImpl implements IOfficialProductService {
    @Autowired
    private OfficialProductMapper officialProductMapper;
    @Autowired
    private OfficialCategoryMapper officialCategoryMapper;

    /**
     * 查询产品
     *
     * @param id 产品主键
     * @return 产品
     */
    @Override
    public OfficialProductVO selectOfficialProductById(Long id) {
        OfficialProduct product = officialProductMapper.selectOfficialProductById(id);
        OfficialProductVO vo = new OfficialProductVO();
        OfficialCategory officialCategory = officialCategoryMapper.selectOfficialCategoryById(product.getCategoryId());
        vo.setCategoryNameZh(officialCategory.getCategoryNameZh());
        vo.setCategoryNameEn(officialCategory.getCategoryNameEn());
        BeanUtils.copyProperties(product, vo);
        if (StringUtils.isNotEmpty(product.getSpecificationsZh())) {
            List<SpecificationDTO> specificationDTOS = JSONArray.parseArray(product.getSpecificationsZh(), SpecificationDTO.class);
            vo.setSpecificationsZh(specificationDTOS);
        }
        if (StringUtils.isNotEmpty(product.getSpecificationsEn())) {
            List<SpecificationDTO> specificationDTOS = JSONArray.parseArray(product.getSpecificationsEn(), SpecificationDTO.class);
            vo.setSpecificationsEn(specificationDTOS);
        }
        return vo;
    }

    /**
     * 查询产品列表
     *
     * @param officialProduct 产品
     * @return 产品
     */
    @Override
    public List<OfficialProduct> selectOfficialProductList(OfficialProduct officialProduct) {
        return officialProductMapper.selectOfficialProductList(officialProduct);
    }

    /**
     * 新增产品
     *
     * @param dto 产品
     * @return 结果
     */
    @Override
    public int insertOfficialProduct(OfficialProductDTO dto) {
        OfficialProduct officialProduct = new OfficialProduct();
        BeanUtils.copyProperties(dto, officialProduct);
        fillSpecifications(dto, officialProduct);
        officialProduct.setCreateTime(DateUtils.getNowDate());
        return officialProductMapper.insertOfficialProduct(officialProduct);
    }

    /**
     * 修改产品
     *
     * @param dto 产品
     * @return 结果
     */
    @Override
    public int updateOfficialProduct(OfficialProductDTO dto) {
        OfficialProduct officialProduct = new OfficialProduct();
        fillSpecifications(dto, officialProduct);
        BeanUtils.copyProperties(dto, officialProduct);
        officialProduct.setUpdateTime(DateUtils.getNowDate());
        return officialProductMapper.updateOfficialProduct(officialProduct);
    }

    private void fillSpecifications(OfficialProductDTO dto, OfficialProduct officialProduct) {
        if (!CollectionUtils.isEmpty(dto.getSpecificationsEn())) {
            List<SpecificationDTO> specificationsEn = dto.getSpecificationsEn();
            String jsonString = JSONArray.toJSONString(specificationsEn);
            officialProduct.setSpecificationsEn(jsonString);
        }
        if (!CollectionUtils.isEmpty(dto.getSpecificationsZh())) {
            List<SpecificationDTO> specificationsZh = dto.getSpecificationsZh();
            String jsonString = JSONArray.toJSONString(specificationsZh);
            officialProduct.setSpecificationsZh(jsonString);
        }
    }

    /**
     * 批量删除产品
     *
     * @param ids 需要删除的产品主键
     * @return 结果
     */
    @Override
    public int deleteOfficialProductByIds(Long[] ids) {
        return officialProductMapper.deleteOfficialProductByIds(ids);
    }

    /**
     * 删除产品信息
     *
     * @param id 产品主键
     * @return 结果
     */
    @Override
    public int deleteOfficialProductById(Long id) {
        return officialProductMapper.deleteOfficialProductById(id);
    }

    @Override
    public void addVisitNumber(Long id) {
        OfficialProduct officialProduct = officialProductMapper.selectOfficialProductById(id);
        officialProduct.setVisitNumber(officialProduct.getVisitNumber() + 1);
        officialProductMapper.updateOfficialProduct(officialProduct);
    }
}
